{ "cells": [ { "cell_type": "markdown", "id": "b294fc10-240d-4ff0-a5ee-33f7ce046d93", "metadata": {}, "source": [ "# Tutorial: Running populations with binary_c-python (Monte-carlo sampling)" ] }, { "cell_type": "markdown", "id": "b33132b0-64c6-4ffa-b027-e212d37d8637", "metadata": {}, "source": [ "WARNING: the Monte-Carlo sampling functionality is still in _beta_-stage and is not fully reliable. This notebook is not finished yet either.\n", "\n", "This notebook will show you how to evolve a population of stars through a Monte-Carlo based sampling method.\n", "\n", "In the [population evolution notebook](https://binary_c.gitlab.io/binary_c-python/examples/notebook_population.html) we explained how to set up a Population object and evolve all the systems with a grid-based sampling method. If you are not familiar yet with the grid-based approach, we recommend studying that notebook first. Here we will use a Monte-Carlo based sampling approach.\n", "\n", "This approach samples systems based on the probability of occurence according to their probability distributions. We thus do not assign a probability to the system, but systems with properties that have a higher probability of occurrence will be sampled more often than others. This is the most standard Monte-Carlo based sampling approach. More advanced features will be introduced later, like Latin-Hypercube sampling and Adaptive Importance sampling." ] }, { "cell_type": "code", "execution_count": 9, "id": "e7128a7e-da26-48b9-bcde-11ba6a329f89", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "from binarycpython.utils.custom_logging_functions import temp_dir\n", "from binarycpython import Population\n", "\n", "TMP_DIR = temp_dir(\"notebooks\", \"notebook_monte_carlo\", clean_path=True)\n", "\n", "# help(Population) # Uncomment to see the public functions of this object" ] }, { "cell_type": "markdown", "id": "4a429c58-2be4-41a1-b0de-475fda7aa455", "metadata": {}, "source": [ "We control which quantities we sample again by using `sampling_variables` like in the [population evolution notebook](https://binary_c.gitlab.io/binary_c-python/examples/notebook_population.html). There are some important things to take care of here, like calculating the multiplicity at the correct point, and introducing `branching_points` at the correct variables.\n", "\n", "Let's look at an example of a single-star population, where we only sample the quantity `M_1`.\n", "\n", "We will first set up the population with the correct settings." ] }, { "cell_type": "code", "execution_count": 18, "id": "75daddea-3709-4507-a2ba-fb3308e66864", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "adding: evolution_type=monte_carlo to grid_options\n", "adding: monte_carlo_mass_threshold=10 to grid_options\n", "adding: _actually_evolve_system=False to grid_options\n", "adding: num_cores=1 to grid_options\n" ] } ], "source": [ "# Create population object\n", "example_pop = Population(tmp_dir=TMP_DIR, verbosity=1)\n", "\n", "# Configure population object\n", "example_pop.set(\n", " # binary_c physics options\n", " multiplicity=1,\n", " evolution_type=\"monte_carlo\",\n", " monte_carlo_mass_threshold=10,\n", " _actually_evolve_system=False, # For now, let's disable actually evolving the systems with binary_c\n", " num_cores=1, # Lets set the number of cores to 1 for now\n", ")" ] }, { "cell_type": "code", "execution_count": 19, "id": "7408bdc9-9f1f-4ab2-9b98-f214056bead6", "metadata": {}, "outputs": [], "source": [ "resolution = {'M_1': 10}\n", "\n", "# add sampling variable for M_1\n", "example_pop.add_sampling_variable(\n", " name=\"M_1\",\n", " longname=\"Primary mass\",\n", " valuerange=[0.06, 300],\n", " samplerfunc=\"10**self.const_linear(np.log10(0.10001), np.log10(300), {})\".format(resolution[\"M_1\"]),\n", " probdist=\"self.three_part_powerlaw(M_1, 0.1, 0.5, 1.0, 301, -1.3, -2.3, -2.3)\",\n", " bottomcode=\"multiplicity = self._sample_multiplicity(system_dict); system_dict['multiplicity'] = multiplicity;\",\n", " dphasevol=\"dlnm1\",\n", " parameter_name=\"M_1\",\n", " branchpoint=1,\n", " branchcode=\"multiplicity == 1\"\n", ")" ] }, { "cell_type": "code", "execution_count": 20, "id": "0960b46c-7389-42ef-99ae-5a12ebed0602", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Warning: No parse function set. Make sure you intended to do this.\n", "Creating and loading custom logging functionality\n", "[2023-01-25 23:51:02,029 DEBUG MainProcess] --- setting up the system_queue_filler now\n", "Write sampling functions code to /tmp/binary_c_python-david/notebooks/notebook_monte_carlo/binary_c_monte_carlo_sampling_sampling_functions_bd5e5d94033f45cb9e26c1cd12ab7e9e.py \n", "Load monte-carlo sampling functions from /tmp/binary_c_python-david/notebooks/notebook_monte_carlo/binary_c_monte_carlo_sampling_sampling_functions_bd5e5d94033f45cb9e26c1cd12ab7e9e.py\n", "Writing sampling variable M_1 to monte-carlo sampling generator\n", "Write grid code to /tmp/binary_c_python-david/notebooks/notebook_monte_carlo/binary_c_monte_carlo_sampling_generator_bd5e5d94033f45cb9e26c1cd12ab7e9e.py \n", "Load monte-carlo generator function from /tmp/binary_c_python-david/notebooks/notebook_monte_carlo/binary_c_monte_carlo_sampling_generator_bd5e5d94033f45cb9e26c1cd12ab7e9e.py\n", "Grid code loaded\n", "[2023-01-25 23:51:02,055 DEBUG Process-28] --- Setting up processor: process-0\n", "[2023-01-25 23:51:02,080 DEBUG MainProcess] --- Signalling processes to stop\n", "Do join of subprocesses ...[2023-01-25 23:51:02,082 DEBUG Process-28] --- Process-0 is finishing.\n", "\n", "process 0 free memory and return \n", "****************************************************\n", "* Process 0 finished: *\n", "* generator started at 2023-01-25T23:51:02.030944 *\n", "* generator finished at 2023-01-25T23:51:02.114483 *\n", "* total: 0.08s *\n", "* of which 0.00s with binary_c *\n", "* Ran 33 systems *\n", "* with a total probability of 33 *\n", "* This thread had 0 failing systems *\n", "* with a total failed probability of 0 *\n", "* Skipped a total of 0 zero-probability systems *\n", "* *\n", "****************************************************\n", "\n", "process 0 queue put output_dict \n", "[2023-01-25 23:51:02,124 DEBUG Process-28] --- Process-0 is finished.\n", "process 0 return \n", "Joined all subprocesses.\n", "**********************************************************\n", "* Population-bd5e5d94033f45cb9e26c1cd12ab7e9e finished! *\n", "* The total probability is 33. *\n", "* It took a total of 0.61s to run 33 systems on 1 cores *\n", "* = 0.61s of CPU time. *\n", "* Maximum memory use 178.703 MB *\n", "**********************************************************\n", "\n", "No failed systems were found in this run.\n", "Do analytics\n", "Added analytics to metadata\n" ] }, { "data": { "text/plain": [ "{'population_id': 'bd5e5d94033f45cb9e26c1cd12ab7e9e',\n", " 'evolution_type': 'monte_carlo',\n", " 'failed_count': 0,\n", " 'failed_prob': 0,\n", " 'failed_systems_error_codes': [],\n", " 'errors_exceeded': False,\n", " 'errors_found': False,\n", " 'total_probability': 33,\n", " 'total_count': 33,\n", " 'start_timestamp': 1674690661.9999769,\n", " 'end_timestamp': 1674690662.6125712,\n", " 'time_elapsed': 0.6125943660736084,\n", " 'total_mass_run': 10.188142293973625,\n", " 'total_probability_weighted_mass_run': 10.188142293973625,\n", " 'zero_prob_stars_skipped': 0}" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example_pop.evolve()" ] }, { "cell_type": "code", "execution_count": null, "id": "1167d549-4d9b-4e7c-9eef-8c0b5650faf1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.9" } }, "nbformat": 4, "nbformat_minor": 5 }